# -*- coding:utf-8 -*-

from weixin.pay import WeixinPay, WeixinPayError
from flask import jsonify, request

wx_pay = WeixinPay(app_id='123', mch_id='111', mch_key='122', notify_url='baidu.com')


class WeixinPay(object):
    def __init__(self):
        pass

    def pay_create(self):
        '''
        微信JSAPI创建统一订单
        :return:
        '''
        try:
            out_trade_no = wx_pay.nonce_str
            raw = wx_pay.jsapi(openid="openid", body=u"测试", out_trade_no=out_trade_no, total_fee=1)
            return jsonify(raw)
        except WeixinPayError, e:
            print e.message
            return e.message, 400
    def pay_nofity(self):
        """
        微信异步通知
        :return:
        """
        data = wx_pay.to_dict(request.data)
        if not wx_pay.check(data):
            return wx_pay.reply("签名验证失败", False)
        # 处理业务逻辑
        return wx_pay.reply("OK", True)